package com.example.mansystem.controller;

import com.example.mansystem.Annotation.RequiredApi;
import com.example.mansystem.dto.ExcelRegisterDto;
import com.example.mansystem.dto.LoginDto;
import com.example.mansystem.dto.RegisterDto;
import com.example.mansystem.dto.UserInfoDto;
import com.example.mansystem.entity.CallResultMsg;
import com.example.mansystem.entity.DepartmentDO;
import com.example.mansystem.entity.RoleDO;
import com.example.mansystem.entity.UserDO;
import com.example.mansystem.exception.CodeAndMsg;
import com.example.mansystem.exception.UniformReponseHandler;
import com.example.mansystem.exception.UserDefinedException;
import com.example.mansystem.service.UserService;
import com.example.mansystem.vo.UserInfo;
import com.example.mansystem.vo.UserVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @GetMapping("/autologin")
    public CallResultMsg userAutoLogin(HttpServletRequest request)
    {
        HttpSession session = request.getSession();
        if(session==null)
            throw new UserDefinedException(CodeAndMsg.NOTLOGIN);
        if(StringUtils.isEmpty(session.getAttribute("username")))
        {
            throw new UserDefinedException(CodeAndMsg.NOTLOGIN);
        }
        String username =(String) session.getAttribute("username");
        List<String> route = userService.getUserRoute(username);
        List<String> api = userService.getUserApi(username);
        UserInfo res = UserInfo.builder().username(username).router_permission(route).api_permission(api).build();
        return new UniformReponseHandler<UserInfo>().sendSuccessResponse(res);
    }

    @GetMapping("/department")
    public CallResultMsg getDepartment(HttpServletRequest request)
    {
        List<DepartmentDO> res = userService.getAllDepartment();
        return new UniformReponseHandler<List<DepartmentDO>>().sendSuccessResponse(res);
    }

    @GetMapping("/alluser")
    public CallResultMsg getAllUser()
    {
        List<UserDO> res=userService.getAllUser();
        for(int i=0;i< res.size();i++)
        {
            res.get(i).setPassword("");
        }
        return new UniformReponseHandler<List<UserDO>>().sendSuccessResponse(res);
    }

    @PostMapping("/login")
    public CallResultMsg userLogin(HttpServletRequest request, @RequestBody @Validated LoginDto validator)
    {
        HttpSession session = request.getSession();
        userService.UserLogin(validator);
        String username =validator.getUsername();
        session.setAttribute("username",username);
        List<String> route = userService.getUserRoute(username);
        List<String> api = userService.getUserApi(username);
        UserInfo res = UserInfo.builder().username(username).router_permission(route).api_permission(api).build();
        return new UniformReponseHandler<UserInfo>().sendSuccessResponse(res);
    }

    @PostMapping("/register")
    public  CallResultMsg userRegister(HttpServletRequest request,@RequestBody @Validated RegisterDto validator)
    {
        if(userService.checkExist(validator.getUsername()))
        {
            throw new UserDefinedException(CodeAndMsg.USER_EXISTED);
        }
        userService.creatUser(validator);
        HttpSession session = request.getSession();
        session.setAttribute("username",validator.getUsername());
        return new UniformReponseHandler<String>().sendSuccessResponse("success");
    }

    @RequiredApi("user:edit")
    @PostMapping("/changeuser")
    public CallResultMsg changeUserInfo(HttpServletRequest request,@RequestBody @Validated UserInfoDto validator)
    {
        userService.UpdateUser(validator);
        return new UniformReponseHandler<String>().sendSuccessResponse("success");
    }

    @PostMapping("/excelregister")
    public CallResultMsg excelRegister(@RequestBody @Validated ExcelRegisterDto dto)
    {
        userService.ExcelRegister(dto);
        return new UniformReponseHandler<String>().sendSuccessResponse("success");
    }
}
